package com.pd.business.impl;

import com.pd.business.RoleService;
import com.pd.dao.mapper.RoleDao;
import com.pd.dao.model.Role;
import com.pd.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Author: dong peng
 * Date: 2018/1/10
 * Time: 11:26
 */

@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    public List<Role> query() {
        List<Role> roleList = roleDao.selectRoles();
        return roleList;
    }

    public Integer add(Role role) {
        if(role == null || role.getRoleValue() == null) {
            throw  new BusinessException("角色值不能为空");
        }
        Role oldRole = roleDao.selectByValue(role.getRoleValue());
        if(oldRole != null) {
            throw  new BusinessException("该角色已存在");
        }
        return roleDao.insertRole(role);
    }

    public Integer modify(Role role) {
        return roleDao.updateRole(role);
    }

    public Integer delete(Integer id) {
        return roleDao.deleteRole(id);
    }
}
